package com.schoolcloud.serve.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.schoolcloud.serve.dto.AttributeGroupAssociatedDTO;
import com.schoolcloud.serve.entity.AttrGroupAssociated;
import com.schoolcloud.serve.service.AttrGroupAssociatedService;
import com.schoolcloud.serve.mapper.AttrGroupAssociatedMapper;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author hongmizfb
* @description 针对表【attr_group_associated(属性分组关联表)】的数据库操作Service实现
* @createDate 2025-04-18 19:11:18
*/
@Service
public class AttrGroupAssociatedServiceImpl extends ServiceImpl<AttrGroupAssociatedMapper, AttrGroupAssociated>
    implements AttrGroupAssociatedService{

    @Override
    public void groupAssociated(AttributeGroupAssociatedDTO dto) {
        String groupId = dto.getGroupId();
        List<String> attributeIds = dto.getAttributeIds();
//        先将当前分组下所有属性删除,然后再执行插入
        lambdaUpdate().eq(AttrGroupAssociated::getGroupId,groupId).remove();
        if (CollUtil.isNotEmpty(attributeIds)){
            for (String attributeId : attributeIds) {
                AttrGroupAssociated attrGroupAssociated = new AttrGroupAssociated();
                attrGroupAssociated.setGroupId(Integer.parseInt(dto.getGroupId()));
                attrGroupAssociated.setAttributeId(Integer.parseInt(attributeId));
                save(attrGroupAssociated);
            }
        }
    }

    @Override
    public List<Integer> getAttributeIdsByGroupId(String groupId) {
        return lambdaQuery().eq(AttrGroupAssociated::getGroupId,groupId).list().stream().map(AttrGroupAssociated::getAttributeId).toList();
    }

    @Override
    public boolean attributeIsAssociated(Integer attributeId) {
       return lambdaQuery().eq(AttrGroupAssociated::getAttributeId,attributeId).exists();
    }
}




